package forTesting;

import baseObjects.Language;
import baseObjects.Symbol;
import baseObjects.XML_Handler;
import corpus.Indexer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.Scanner;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
import pipeline.KeyboardConsumer;
import pipeline.KeyboardProducer;
import pipeline.SynchronizedBuffer;
import pipeline.TopBuffer;

public class AnoterTestClass {

    /**
     * @param args
     * @throws Exception
     * @throws FileNotFoundException
     */
    public static void main (String[] args) throws FileNotFoundException, Exception {
        // TODO Auto-generated method stub
        XML_Handler xmlh=new XML_Handler();

        Language l=(Language)xmlh.readfile("a.xml");
//        System.out.println(l);
//        Vector<Symbol> svect = new Vector<Symbol>();
//        for (Symbol symbol : l2.getSymbolsVect()) {
//            svect.add(new Symbol(symbol));
//        }
//        Language l = new Language();
//        l.setSymbolsVect(svect);
//        System.out.println(l.getSymbolsVect().size());
        Indexer indexer = new Indexer(l, "corpus");
        Obs obs = new Obs();
        indexer.addObserver(obs);
        long t = System.nanoTime();
        indexer.start();
        while(indexer.isAlive());
//        xmlh.writeToFile("a.xml", l);
//        System.out.println(l);
        System.out.println("time : "+TimeUnit.SECONDS.convert((System.nanoTime()-t), TimeUnit.NANOSECONDS));
        System.out.println(indexer);
        indexer = null;
        System.out.println("Sorted Symbols:");
        for (Symbol val : l.getSymbolsSortet()) {
            System.out.println(val.getRegular() + " : "+val.getProbobility());
        }
        System.out.println("press a key");
//        new Scanner(System.in).next();
        Properties p = new Properties();
        p.load(new FileInputStream(new File("keyPriorityGeneric.properties")));
//        p.load(new FileInputStream(new File("keyPriority.properties")));
        System.out.println(l);
        SynchronizedBuffer sb = new SynchronizedBuffer();
        KeyboardProducer kbp = new KeyboardProducer(sb, l, p);
        System.out.println("kbp done");
        kbp.setGeneric(true);
//        kbp.setGeneric(false);
        TopBuffer buff = new TopBuffer(10);
        buff.addObserver(obs);
        //kbp.addObserver(obs);
        Vector<KeyboardConsumer> consumers = new Vector<KeyboardConsumer>();
        for (int i =0; i<5; i++){
            KeyboardConsumer kbc = new KeyboardConsumer(sb, buff, "consumerCorpus", l);
            kbc.setName("KBC "+i);
            consumers.add(kbc);
            kbc.addObserver(obs);
            kbc.start();
        }
        kbp.start();
        while (kbp.isAlive()) {
            Thread.currentThread().sleep(1000);
        }
//        System.out.println(l.getSymbolsSortet());


    }
}
